<?php

declare(strict_types=1);

namespace App\Job;

use App\Helpers\ImUtil;
use App\Helpers\RedisUtil;
use App\Model\GiftLog;
use App\Model\Message;
use App\Model\User;
use App\Model\UserProfile;
use Hyperf\AsyncQueue\Job;

/**
 * 聊天对话信息任务
 */
class ChatInfoJob extends Job
{
    /**
     * 参数
     */
    public $params;

    /**
     * 任务执行失败后的重试次数，即最大执行次数为 $maxAttempts+1 次
     *
     * @var int
     */
    protected $maxAttempts = 0;

    public function __construct($params)
    {
        $this->params = $params;
    }

    public function handle()
    {
        $userId = $this->params['user_id'] ?? 0;
        $chatUserId = $this->params['chat_user_id'] ?? 0;
        log_user_result($userId, "ChatUid:{$chatUserId}");
        if (!$userId || !$chatUserId) return;
        $isHost = $this->params['is_host'];
        $selfProfile = $this->params['self_profile'];
        $chatProfile = $this->params['chat_profile'];
        $selfImAccount = $this->params['self_im_account'];
        $chatImAccount = $this->params['chat_im_account'];
        if ($isHost) {
            //礼物过期提示
            $ret['exp_num'] = GiftLog::expireRecords($chatUserId, $userId);
            if ($ret['exp_num'] > 0) {
                ImUtil::sendInfoTipMsg($selfImAccount, '您没有及时回复消息，之前的部分礼物已被退回～', $chatImAccount);
            }
        }
        //资料卡
        $ret = $this->doSendProfileCard($userId, $chatUserId, $chatProfile, $selfProfile);
        log_user_result($userId, "ChatUid:{$chatUserId}==Ret:{$ret}");
        return;
    }

    /**
     * 发送名片卡
     */
    function doSendProfileCard($userId = 0, $chatUserId = 0, $chatProfile = null, $selfProfile = null)
    {
        if (
            !$selfProfile
            || !$chatProfile
            || $selfProfile->gender == -1
            || $selfProfile->gender == $chatProfile->gender
        ) return 0;

        //版本
        $versions = User::checkVersion([$userId, $chatUserId]);
        // if ($versions[$userId] < '1.3.1' || $versions[$chatUserId] < '1.3.1') return 0;

        $key = "chat_show_card_{$userId}_{$chatUserId}";
        // if (RedisUtil::get($key) == 1) return 0;
        // RedisUtil::set($key, 1, 3600 * 24);
        //发送资料卡
        if ($versions[$userId] >= '1.3.3') {
            $ret = Message::sendNewProfileCard($userId, $chatUserId, $chatProfile);
        } else {
            $ret = Message::sendProfileCard($userId, $chatUserId, $chatProfile);
        }
        return $ret;
    }

    /**
     * 真人认证提示
     */
    // function sendVerifyTip($userId = 0, $chatUserId = 0, $chatProfile = null, $isOfficialChat = 0)
    // {
    // if ($this->curVersion < '1.2.7' || $isOfficialChat) return false;
    //     $vryOk = UserProfile::checkAvatarVrfied($chatUserId, $chatProfile);
    //     if (!$vryOk) return;
    //     $key = "verify_tip_{$userId}_{$chatUserId}";
    //     if (RedisUtil::get($key) == 1) return;
    //     RedisUtil::set($key, 1, 3600 * 72);
    //     Message::sendVryOKTip($userId, $chatUserId);
    // }
}
